package com.yuandian.ops.workbench.controller;

import cn.hutool.core.date.DateUtil;
import com.yuandian.base.service.ITaskDataService;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.ops.workbench.po.AlarmListParams;
import com.yuandian.ops.workbench.service.IDevOpsHomePageService;
import com.yuandian.ops.workbench.vo.MonitorOverviewVo;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.vo.TrendDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;

/**
 * 运维工作台首页 Controller
 *
 * @author LinShiHe
 * @date 2023/03/12
 */
@RestController
@RequestMapping("/ywgzt/home")
@Api(tags = "首页大屏-Controller")
public class DevOpsHomePageController {

	@Autowired
	private IDevOpsHomePageService homePageService;

	@Autowired
	private ITaskDataService taskDataService;

	/**
	 * 获取首页头部的统计数据
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "首页大屏顶部系统概况", notes = "首页大屏顶部系统概况")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "dateTime", value = "选择日期", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/headerInfo")
	public R<MonitorOverviewVo> getHeaderInfo(@Validated @RequestBody AlarmListParams params) {
		MonitorOverviewVo moVo = homePageService.queryRecentMonitorOverview(params);
		return R.ok(moVo);
	}

	/**
	 * 查询区域业务统计（地图）
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "首页大屏查询区域业务统计（地图）", notes = "首页大屏查询区域业务统计（地图）")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping(value = "/queryMapAreaBusinessStatistics")
	public R<Object> queryMapAreaBusinessStatistics(@RequestBody AlarmListParams params) {
		return homePageService.queryMapAreaBusinessStatistics(params);
	}

	/**
	 * 查询区域业务统计(表格数据)
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "首页-中间地图右下角表格-地区告警信息", notes = "首页-中间地图右下角表格-地区告警信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/queryAreaBusinessStatistics")
	public R<Object> queryAreaBusinessStatistics(@RequestBody AlarmListParams params) {
		return homePageService.queryAreaBusinessStatistics(params);
	}

	/**
	 * 查询系统受理量、告警量趋势
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "首页-中间底部-系统趋势信息折线图", notes = "首页-中间底部-系统趋势信息折线图")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统Id", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/queryCompleteAlarmTrend")
	public R<Object> queryCompleteAlarmTrend(@RequestBody AlarmListParams params) throws ParseException {

		TrendDataVo trendDataVo = taskDataService.getTrendObject("详单表分钟统计", params.getStartTime(), params.getEndTime(), null);
		if (trendDataVo.isLastShow()){
			params.setEndTime(DateUtil.formatDateTime(new Date(DateUtil.parse(trendDataVo.getTrendEndTime()).getTime() + 60*1000)));
		}
		return homePageService.queryCompleteAlarmTrend(params);
	}

	/**
	 * 首页-右上角-业务告警TOP5列表
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "首页-右上角-业务告警TOP5列表", notes = "首页-右上角-业务告警TOP5列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统Id", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/business/busiAlarmTop")
	public R<Object> getBusinessBusiAlarmTop(@RequestBody AlarmListParams params) {
		return homePageService.getBusinessBusiAlarmTop(params);
	}


	/**
	 * 首页-右上角-’关键业务‘ 健康度 TOP5列表
	 *
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "首页-右上角-’关键业务‘ 健康度 TOP5列表", notes = "首页-右上角-’关键业务‘ 健康度 TOP5列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统Id", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/business/busiHealthTop")
	public R<Object> getBusinessHealthTop(@RequestBody AlarmListParams params) {

		return homePageService.getBusinessHealthTop(params);
	}

	/**
	 * (BPM 4.0)
	 * 查询最新健康度告警topN
	 * @param params 查询业务告警信息相关数据请求对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "查询最新健康度告警topN", notes = "查询最新健康度告警topN")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "systemId", value = "系统Id", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "query", required = true, dataType = "Long")
	})
	@PostMapping("/queryRecentHealthRateAlarmTopN")
	public R<Object> queryRecentHealthRateAlarmTopN(@RequestBody AlarmListParams params) {
		PageDataInfo pageDataInfo = homePageService.queryRecentHealthRateAlarmTopN(params);
		return R.ok(pageDataInfo);
	}

}
